#include "trackingobject.h"

TrackingObject::TrackingObject()
{
}

TrackingObject::TrackingObject(const TrackingObject& _obj)
{
    this->id = _obj.id;
    this->rect = _obj.rect;
    this->color = _obj.color;
    Trace(_obj.trace);
}

TrackingObject::TrackingObject(int _id, CvScalar _color, CvRect _rect)
{
    id = _id;
    color = _color;
    rect = _rect;
    trace.clear();
    trace.append(cvPoint(
                     _rect.x + _rect.width/2,
                     _rect.y + _rect.height/2
                     )
                 );
}

int TrackingObject::ID()
{
    return id;
}

int TrackingObject::ID(int _id)
{
    id = _id;
    return id;
}


CvRect TrackingObject::Rect()
{
    return rect;
}

void TrackingObject::Rect(CvRect _rect)
{
    rect = _rect;
}


CvScalar TrackingObject::Color()
{
    return color;
}

void TrackingObject::Color(int val0, int val1, int val2)
{
    color = cvScalar(val0, val1, val2);
}

void TrackingObject::Color(CvScalar _color)
{
    color = _color;
}

QList<CvPoint> TrackingObject::Trace()
{
    return trace;
}

void TrackingObject::Trace(QList<CvPoint> _trace)
{
    trace.clear();
    trace.append(_trace);
}

void TrackingObject::appendTrace(CvPoint p)
{
    trace.append(p);
}

CvPoint TrackingObject::center()
{
    return cvPoint(
                rect.x + rect.width/2,
                rect.y + rect.height/2
                );
}

CvPoint TrackingObject::center(CvRect re)
{
    return cvPoint(
                re.x + re.width/2,
                re.y + re.height/2
                );
}

TrackingObject TrackingObject::operator =(TrackingObject o)
{
    TrackingObject trakingObject;
    trakingObject.ID(o.ID());
    trakingObject.Color(o.Color());
    trakingObject.Rect(o.Rect());
    trakingObject.Trace(o.Trace());

    return trakingObject;
}

CvPoint TrackingObject::predictedCenter()
{
    if(trace.length() > 1)
    {
        return cvPoint(
                    2*trace.last().x - trace[trace.length() - 2].x,
                    2*trace.last().y - trace[trace.length() - 2].y
                    );
    }else
        return center();

}

TrackingObject TrackingObject::predictedObject()
{
    return TrackingObject(
                this->id,
                this->color,
                cvRect(
                    predictedCenter().x - this->rect.width/2,
                    predictedCenter().y - this->rect.height/2,
                    this->rect.width,
                    this->rect.height
                    )
                );
}

void TrackingObject::update(CvRect _rect)
{
    rect = _rect;
    trace.append(center());
}
